package com.mpgame.database.mysql.mapper.game;

import com.mpgame.database.mysql.mapper.BaseMapper;
import com.mpgame.database.mysql.model.game.MonsterHotbar;
import com.mpgame.database.mysql.model.game.MonsterStore;
import com.mpgame.database.mysql.model.game.custom.MonsterDisplayInfo;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface MonsterHotbarMapper extends BaseMapper<MonsterHotbar> {
    
    @Insert("INSERT INTO monster_hotbar (player_id, hotbar1_monster_store_id, hotbar2_monster_store_id, hotbar3_monster_store_id, hotbar4_monster_store_id, hotbar5_monster_store_id, hotbar6_monster_store_id) VALUES (#{playerId}, #{hotbar1MonsterStoreId}, #{hotbar2MonsterStoreId}, #{hotbar3MonsterStoreId}, #{hotbar4MonsterStoreId}, #{hotbar5MonsterStoreId}, #{hotbar6MonsterStoreId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(MonsterHotbar monsterHotbar);

    @Select("SELECT * FROM monster_hotbar WHERE id = #{id}")
    MonsterHotbar selectById(@Param("id") int id);

    @Select("SELECT * FROM monster_hotbar WHERE player_id = #{playerId}")
    MonsterHotbar selectByPlayerId(@Param("playerId") int playerId);

    @Select("SELECT * FROM monster_hotbar")
    List<MonsterHotbar> selectAll();

    @Update("UPDATE monster_hotbar SET hotbar1_monster_store_id = #{hotbar1MonsterStoreId}, hotbar2_monster_store_id = #{hotbar2MonsterStoreId}, hotbar3_monster_store_id = #{hotbar3MonsterStoreId}, hotbar4_monster_store_id = #{hotbar4MonsterStoreId}, hotbar5_monster_store_id = #{hotbar5MonsterStoreId}, hotbar6_monster_store_id = #{hotbar6MonsterStoreId} WHERE id = #{id}")
    int updateById(MonsterHotbar monsterHotbar);

    /** 根据玩家id查询到该玩家hotbar中的怪物信息列表 */
    @Select("SELECT ms.* FROM monster_store ms " +
            "INNER JOIN monster_hotbar mh ON ms.id IN (mh.hotbar1_monster_store_id, mh.hotbar2_monster_store_id, mh.hotbar3_monster_store_id, mh.hotbar4_monster_store_id, mh.hotbar5_monster_store_id, mh.hotbar6_monster_store_id) " +
            "WHERE mh.player_id = #{playerId} AND ms.id > 0")
    List<MonsterStore> selectMonsterStoresByPlayerId(@Param("playerId") int playerId);
    /**
     * 根据玩家ID和槽位索引获取指定的MonsterStore
     */
    @Select("SELECT ms.* FROM monster_store ms " +
            "INNER JOIN monster_hotbar mh ON ms.id = " +
            "CASE #{slotIndex} " +
            "WHEN 0 THEN mh.hotbar1_monster_store_id " +
            "WHEN 1 THEN mh.hotbar2_monster_store_id " +
            "WHEN 2 THEN mh.hotbar3_monster_store_id " +
            "WHEN 3 THEN mh.hotbar4_monster_store_id " +
            "WHEN 4 THEN mh.hotbar5_monster_store_id " +
            "WHEN 5 THEN mh.hotbar6_monster_store_id " +
            "END " +
            "WHERE mh.player_id = #{playerId} AND ms.id > 0")
    MonsterStore selectMonsterStoreByPlayerIdAndSlot(@Param("playerId") int playerId, @Param("slotIndex") int slotIndex);



    /**
     * 根据玩家ID获取热槽位中的完整怪物展示信息（包含怪物和技能信息）
     */
    @Select("SELECT " +
            "ms.id as storeId, " +
            "ms.monster_id as monsterId, " +
            "ms.player_id as playerId, " +
            "ms.lv, " +
            "ms.wisdom, " +
            "ms.melee_atk as meleeAtk, " +
            "ms.magic_atk as magicAtk, " +
            "ms.melee_def as meleeDef, " +
            "ms.magic_def as magicDef, " +
            "ms.speed, " +
            "ms.health, " +
            "m.name as monsterName, " +
            "m.family_id as familyId, " +
            "m.description as monsterDescription, " +
            "ms.skill1_id as skill1Id, " +
            "s1.name as skill1Name, " +
            "s1.power as skill1Power, " +
            "s1.pp as skill1Pp, " +
            "s1.description as skill1Description, " +
            "ms.skill2_id as skill2Id, " +
            "s2.name as skill2Name, " +
            "s2.power as skill2Power, " +
            "s2.pp as skill2Pp, " +
            "s2.description as skill2Description, " +
            "ms.skill3_id as skill3Id, " +
            "s3.name as skill3Name, " +
            "s3.power as skill3Power, " +
            "s3.pp as skill3Pp, " +
            "s3.description as skill3Description, " +
            "ms.skill4_id as skill4Id, " +
            "s4.name as skill4Name, " +
            "s4.power as skill4Power, " +
            "s4.pp as skill4Pp, " +
            "s4.description as skill4Description " +
            "FROM monster_store ms " +
            "INNER JOIN monster_hotbar mh ON ms.id IN (mh.hotbar1_monster_store_id, mh.hotbar2_monster_store_id, mh.hotbar3_monster_store_id, mh.hotbar4_monster_store_id, mh.hotbar5_monster_store_id, mh.hotbar6_monster_store_id) " +
            "INNER JOIN monster m ON ms.monster_id = m.id " +
            "LEFT JOIN skills s1 ON ms.skill1_id = s1.id " +
            "LEFT JOIN skills s2 ON ms.skill2_id = s2.id " +
            "LEFT JOIN skills s3 ON ms.skill3_id = s3.id " +
            "LEFT JOIN skills s4 ON ms.skill4_id = s4.id " +
            "WHERE mh.player_id = #{playerId} AND ms.id > 0")
    List<MonsterDisplayInfo> selectMonsterDisplayInfosByPlayerId(@Param("playerId") int playerId);

    /**
     * 根据玩家ID和槽位索引获取指定的完整怪物展示信息
     */
    @Select("SELECT " +
            "ms.id as storeId, " +
            "ms.monster_id as monsterId, " +
            "ms.player_id as playerId, " +
            "ms.lv, " +
            "ms.wisdom, " +
            "ms.melee_atk as meleeAtk, " +
            "ms.magic_atk as magicAtk, " +
            "ms.melee_def as meleeDef, " +
            "ms.magic_def as magicDef, " +
            "ms.speed, " +
            "ms.health, " +
            "m.name as monsterName, " +
            "m.family_id as familyId, " +
            "m.description as monsterDescription, " +
            "ms.skill1_id as skill1Id, " +
            "s1.name as skill1Name, " +
            "s1.power as skill1Power, " +
            "s1.pp as skill1Pp, " +
            "s1.description as skill1Description, " +
            "ms.skill2_id as skill2Id, " +
            "s2.name as skill2Name, " +
            "s2.power as skill2Power, " +
            "s2.pp as skill2Pp, " +
            "s2.description as skill2Description, " +
            "ms.skill3_id as skill3Id, " +
            "s3.name as skill3Name, " +
            "s3.power as skill3Power, " +
            "s3.pp as skill3Pp, " +
            "s3.description as skill3Description, " +
            "ms.skill4_id as skill4Id, " +
            "s4.name as skill4Name, " +
            "s4.power as skill4Power, " +
            "s4.pp as skill4Pp, " +
            "s4.description as skill4Description " +
            "FROM monster_store ms " +
            "INNER JOIN monster_hotbar mh ON ms.id = " +
            "CASE #{slotIndex} " +
            "WHEN 0 THEN mh.hotbar1_monster_store_id " +
            "WHEN 1 THEN mh.hotbar2_monster_store_id " +
            "WHEN 2 THEN mh.hotbar3_monster_store_id " +
            "WHEN 3 THEN mh.hotbar4_monster_store_id " +
            "WHEN 4 THEN mh.hotbar5_monster_store_id " +
            "WHEN 5 THEN mh.hotbar6_monster_store_id " +
            "END " +
            "INNER JOIN monster m ON ms.monster_id = m.id " +
            "LEFT JOIN skills s1 ON ms.skill1_id = s1.id " +
            "LEFT JOIN skills s2 ON ms.skill2_id = s2.id " +
            "LEFT JOIN skills s3 ON ms.skill3_id = s3.id " +
            "LEFT JOIN skills s4 ON ms.skill4_id = s4.id " +
            "WHERE mh.player_id = #{playerId} AND ms.id > 0")
    MonsterDisplayInfo selectMonsterDisplayInfoByPlayerIdAndSlot(@Param("playerId") int playerId, @Param("slotIndex") int slotIndex);
}
